/**
 * @class luffy.common.PopupLayer
 * 마크업을 이용한 팝업 레이어를 제공한다.
 */
luffy.common.DimmedLayer = jindo.$Class({
	
	$init : function(sClassName){
		this.welWrapLayer = jindo.$Element(jindo.$("<div height='100%' width='100%' style='position:absolute;left:0px;top:0px;z-index:30000;opacity:0.2;background: none repeat scroll 0 0 #000000;filter: alpha(opacity=20);'>"));
		this.PopupLayer = new luffy.common.PopupLayer('dimmedMessage');
		this.welMessageLayer = jindo.$Element(this.PopupLayer.getElement());
		this.setClassName(sClassName);
		
		this.bubbler = new luffy.common.Bubbler(this.welMessageLayer.$value());
		this.bubbler.attach({
			'closeButton:click' : jindo.$Fn(this.closeLayer, this).bind()
		});
		
		document.body.appendChild(this.welWrapLayer.$value());
		
		this.sCloseButton = "<div class='closeButton' style='text-align:center;padding:30px 0px 10px 0px'><input type='button' value='close'></div>";
		this.closeButton = jindo.$(this.sCloseButton);
	},
	
	/**
	 * htmlElement또는 htmlString을 해당 point에 팝업으로 렌더링 한다.
	 */
	openLayer : function(objOrString) {		
		this.PopupLayer.openPopup(objOrString,this._getMessagePoint(objOrString));
		this._showLayer();
	},
	/**
	 * htmlElement또는 htmlString을 해당 point에 팝업으로 렌더링 한다.
	 */
	openLayerInTime : function(objOrString,nMiliSeconds) {
		if(this._oTimer){
			this.closeLayer();
			clearTimeout(this._oTimer);
		}
		this.PopupLayer.openPopup(objOrString,this._getMessagePoint(objOrString));
		this._showLayer();
		
		var self=this;
		this._oTimer = setTimeout(function(){
			self.closeLayer();
		},nMiliSeconds);
		
		
	},
	/**
	 * htmlElement또는 htmlString을 해당 point에 팝업으로 렌더링 한다.
	 */
	openLayerWithButton : function(objOrString) {	
		if(objOrString.appendChild){
			objOrString.appendChild(this.closeButton);
		} else{
			objOrString = objOrString+this.sCloseButton;
		}
		this.PopupLayer.openPopup(objOrString,this._getMessagePoint(objOrString));
		this._showLayer();
	},
	/**
	 * 팝업 엘리먼트를 감춘다.
	 * 크기 계산시에 hidden상태여야 가능하기 때문에 hidden으로 처리한다.
	 */
	closeLayer : function(){
		this.PopupLayer.closePopup();
		this.welWrapLayer.hide();
	},
	
	setClassName : function(sClassName){
		this.welWrapLayer.$value().className = sClassName || 'dimmedLayer'; 
	},	
	_showLayer : function(){
		var oDocSize = jindo.$Document().scrollSize();
		this.welWrapLayer.css({
			"width" : oDocSize.width+"px",
			"height" : oDocSize.height+"px"
			
		});
		this.welWrapLayer.show();
	},
	/**
	 * point를 기준으로 팝업 사이즈를 계산하여, 잘리지 않는 위치로 재조정한 포인트를 반환한다.
	 */
	_getMessagePoint : function(objOrString){
		var oDocSize = jindo.$Document().scrollSize();
		var oClientSize = jindo.$Document().clientSize();
		var oScrollSize = jindo.$Document().scrollPosition();
		this.welMessageLayer.html(objOrString);
		var oPoint = {
				"x" : oClientSize.width/2 - this.welMessageLayer.width()/2 + oScrollSize.left,
				"y" : oClientSize.height/2 - this.welMessageLayer.height()/2 + oScrollSize.top
		}
		return oPoint;
	}	
});
